package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDTO;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        //根据id查询计划信息
        NursingPlan plan = this.getById(id);
        //将plan信息转换成planVo
        BeanUtils.copyProperties(plan,nursingPlanVo);

        //查询计划中包含的服务信息
        List<NursingProjectPlan> list = nursingProjectPlanService
                .lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        //将信息转换成NuringProjectPlanVo
        List<NursingProjectPlanVo> nursingProjectPlanVos = list.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(projectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setId(Long.valueOf(projectPlan.getId().toString()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(projectPlan.getPlanId().toString()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        //使用tolist转换为list会出现jdk11不兼容问题
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }


    /**
     * 修改护理计划
     * 
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan( NursingPlanDTO nursingPlanDTO)
    {
        //先更新护理计划项目表
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        if (projectPlans!=null && !projectPlans.isEmpty()){
            //删除护理计划中的项目对应的数据
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,nursingPlanDTO.getId()));
            projectPlans.forEach(projectPlan ->projectPlan.setPlanId(Long.valueOf(nursingPlanDTO.getId().toString())));
            //添加新的护理项目
        }
        nursingProjectPlanService.saveBatch(projectPlans);

        //更新护计划表
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
        return nursingPlanMapper.updateById(nursingPlan);

    }



    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //根据id删除对应的护理计划项目表中对应计划的项目数据
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        //删除计划
        int i = nursingPlanMapper.deleteById(id);
        return i>0?1:0;
    }
    /**
     * 添加护理计划
     * @param nursingPlanDTO
     */
    @Override
    public void addNursingPlan(NursingPlanDTO nursingPlanDTO) {
        //添加护理计划
        //1.添加计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
        this.save(nursingPlan);
        //2.添加计划项目
        //2.1先完善信息
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        if (projectPlans!=null && projectPlans.size()>0){
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        }
        nursingProjectPlanService.saveBatch(projectPlans);
    }
}
